package io.renren.modules.title.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.modules.title.dao.ExamOptionsMapper;
import io.renren.modules.title.dao.ExamTitleMapper;
import io.renren.modules.title.entity.ExamOptions;
import io.renren.modules.title.entity.ExamTitle;
import io.renren.modules.title.from.TitleFrom;
import io.renren.modules.title.service.ExamTitleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExamTitleImpl
 * @Author mengjiafeng
 * @Date 2019/9/4 10:37
 **/
@Service
public class ExamTitleImpl extends ServiceImpl<ExamTitleMapper, ExamTitle> implements ExamTitleService {
    @Autowired
    private ExamTitleMapper examTitleMapper;
    @Autowired
    private ExamOptionsMapper examOptionsMapper;


    @Override
    public Map getTitleDetail(String id) {
        HashMap<Object, Object> titleMap = new HashMap<>();
        ExamTitle examTitle = examTitleMapper.selectById(id);
        List<ExamOptions> optionsList = examOptionsMapper.selectList(new QueryWrapper<ExamOptions>().eq("qid", id));
        titleMap.put("examTitle",examTitle);
        titleMap.put("optionsList",optionsList);
        return titleMap;
    }

    @Override
    @Transactional
    public void updateTitle(TitleFrom titleFrom) {
        ExamTitle examTitle = new ExamTitle();
        BeanUtils.copyProperties(titleFrom,examTitle);
        examTitleMapper.updateById(examTitle);
        for (Object o : titleFrom.getOptions()) {
            examOptionsMapper.updateById((ExamOptions) o);
        }
    }
}
